package com.shop.mall.controller;

import com.shop.mall.common.Result;
import com.shop.mall.entity.Product;
import com.shop.mall.entity.User;
import com.shop.mall.service.ProductService;
import com.shop.mall.service.UserService;
import com.shop.mall.utils.Md5Util;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;


    @PostMapping("/add")
    @Operation(summary = "添加商品")
    public Result<String> add(@RequestBody Product product) {
        boolean result = productService.save(product);
        if (!result) {
            return Result.error("添加失败");
        }
        return Result.success();
    }


    @GetMapping("/getById")
    @Operation(summary = "根据id查询商品信息")
    public Result<Product> getById(@RequestParam int id) {
        Product byId = productService.getById(id);
        if (byId == null) {
            return Result.error("获取商品信息失败");
        }
        return Result.success(byId);
    }


    @PutMapping("/update")
    @Operation(summary = "修改商品")
    public Result<String> update(@RequestBody Product product) {
        boolean result = productService.updateById(product);
        if (!result) {
            return Result.error("修改失败");
        }
        return Result.success();
    }
    
    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除商品")
    public Result<String> delete(@PathVariable int id) {
        boolean result = productService.removeById(id);
        if (!result) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
